<?php
//原帖子地址 https://yaohuo.me/bbs-1355041.html?lpage=4
// 简单的修复了下代码,支持mac和linux使用.
function uploadtoqihoo($img, $imginfo, $imgtype) {
    $url = "https://kuaichuan.360kuai.com/upload/img";
    $name = getfilenamebymimetype($imginfo);
    $boundary = '----webkitformboundary' . uniqid();
    $data = "--$boundary\r\n";
    $data .= "content-disposition: form-data; name=\"img\"; filename=\"$name\"\r\n";
    $data .= "content-type: $imgtype\r\n\r\n";
    $data .= file_get_contents($img) . "\r\n";
    $data .= "--$boundary--\r\n";

    // 初始化 curl
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // 设置请求头，包括 boundary
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "content-type: multipart/form-data; boundary=$boundary",
        'host: kuaichuan.360kuai.com',
        'connection: keep-alive',
        'content-length: ' . strlen($data),
        'accept: application/json, text/plain, */*',
        'sec-ch-ua: "chromium";v="122", "not(a:brand";v="24", "google chrome";v="122"',
        'sec-ch-ua-mobile: ?0',
        'user-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/122.0.6261.95 safari/537.36 qihu 360ee',
        'sec-ch-ua-platform: "windows"',
        'origin: https://kuaichuan.360kuai.com',
        'sec-fetch-site: same-origin',
        'sec-fetch-mode: cors',
        'sec-fetch-dest: empty',
        'referer: https://kuaichuan.360kuai.com/',
        'accept-encoding: gzip, deflate, br',
        'accept-language: zh-cn,zh;q=0.9',
        'cookie: 填写cookie',
        // 登录https://kuaichuan.360kuai.com/  可获取cookie
    ]);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        return ['message' => 'curl error: ' . curl_error($ch)];
    }

    curl_close($ch);
    $responsedata = json_decode($response, true);
    if (isset($responsedata['data']['url'])) {
        $url = $responsedata['data']['url'];
        if (strpos($url, 'http://') === 0) {
            $url = str_replace('http://', 'https://', $url);
        }
        return ['message' => $url];
    } else {
        return ['message' => '未找到 data.url'];
    }


}

function getfilenamebymimetype($mimetype) {
    $extension = '';
    if (strpos($mimetype, 'image/jpeg') !== false || strpos($mimetype, 'image/jpg') !== false) {
        $extension = 'jpg';
    } elseif (strpos($mimetype, 'image/png') !== false) {
        $extension = 'png';
    }
    return uniqid() . '.' . $extension;
}

// 处理表单提交
$responsedata = ['message' => ''];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
    $img = $_FILES['image']['tmp_name'];
    $imginfo = $_FILES['image']['type'];
    $imgsize = $_FILES['image']['size'];

    // 检查文件大小和类型
    $allowedtypes = ['image/jpeg', 'image/jpg', 'image/png'];
    if ($imgsize > 10 * 1024 * 1024) {
        $responsedata['message'] = '文件大小不能超过10mb';
    } elseif (!in_array($imginfo, $allowedtypes)) {
        $responsedata['message'] = '只支持jpeg、jpg、png格式的图片';
    } else {
        $responsedata = uploadtoqihoo($img, $imginfo, $imginfo);
    }
}
?>
<!doctype html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" *
    "width=device-width, initial-scale=1.0">
    <meta name="referrer" *
    "no-referrer">
    <title>上传图片到360快传</title>
</head>
<body>
<h1>上传图片</h1>
<form action="" method="post" enctype="multipart/form-data">
    <label for="image">选择图片上传（仅限jpeg、jpg、png，最大10mb）：</label>
    <input type="file" name="image" id="image" accept=".jpg,.jpeg,.png" required>
    <button type="submit">上传</button>
</form>

<h2>响应消息：</h2>
<textarea rows="10" cols="50" readonly>
        <?php echo htmlspecialchars($responsedata['message']); ?>
    </textarea>
</body>
</html>
